<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-08-18 16:40:16
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2022-03-26 22:43:08
 */

namespace app\admin\controller;
use app\common\controller\AdminController;

class MerchantRole extends AdminController {

    protected function initialize(): void {
		parent::initialize();
		$this->saveAndGetConfigIdentify('', '商家角色', '', 'merchantRole');

		$this->merchantRoleDao = model('MerchantRole');
		$this->merchantRuleDao = model('MerchantRule');
		$this->merchantAccessDao = model('MerchantAccess');
		$this->merchantNodeDao = model('MerchantNode');
		$this->merchantNodeAccessDao = model('MerchantNodeAccess');
	}

	public function index() {
		//获取列表
		$map = $parameter = array();
		$count = $this->merchantRoleDao->countSearch($map);
		$Page = new \org\util\PageBootstrap($count, config('app.per_page'), $parameter);
		$pageShow = $Page->show();
		list($list) = $this->merchantRoleDao->search($map, $Page->getLimit());
		//print_r($list);exit;
		$this->assign('count', $count);
		$this->assign('pageShow', $pageShow);
		$this->assign('list', $list);

		return $this->fetch();
	}

	public function listorders() {
		$result = parent::listorders($this->merchantRoleDao);

		if ($result !== false) {
			$this->success('排序更新成功');
		} else {
			$this->error('排序更新失败');
		}
	}

	public function add() {
		$id = input('param.id', '', 'intval');

		if ($id) {
			$info = $this->merchantRoleDao->getInfo($id);
			//print_r($info);
		}
		$this->assign('id', $id);
		$this->assign('info', $info);

		return $this->fetch();
	}

	public function doAdd() {
		$id = input('post.id', '', 'intval');
		$data = input('post.data/a', '', 'pwEscape');

		if ($id) {
			$info = $this->merchantRoleDao->getInfo($id);
		}

		if ($id) {
			$result = $this->merchantRoleDao->baseUpdateData($id, $data);
			if ($result !== false) {
				$this->success('保存成功', url('admin/merchantRole/index'));
			} else {
				$this->error('保存失败'.showDbError($this->merchantRoleDao));
			}
		} else {
			$result = $this->merchantRoleDao->baseAddData($data);
			if ($result !== false) {
				$this->success('添加成功', url('admin/merchantRole/index'));
			} else {
				$this->error('添加失败'.showDbError($this->merchantRoleDao));
			}
		}
	}

	public function access() {
		$roleid = input('param.id', '', 'intval');

		if ($roleid) {
			$info = $this->merchantRoleDao->get($roleid);
		}
		$this->assign('roleid', $roleid);

		//根据角色获取菜单的树
		list($menutree) = $this->merchantRuleDao->getNewMenuTree($roleid);
		$this->assign('menutree', $menutree);
		
		return $this->fetch();
	}

	public function doAccess() {
		$roleid = input('post.roleid', '', 'intval');

		$menu_auth = input('post.menu_auth/a', '', 'pwEscape'); //获取所有授权菜单

		//access权限处理
		$this->merchantAccessDao->where(array('roleid'=>$roleid))->delete();

		$ruleList = $this->merchantRuleDao->where('id', 'in', $menu_auth)->select();
		$ruleList = $ruleList->toArray();

		$data = $dataCid = array();
		foreach ($ruleList as $key => $value) {
			$data[] = array(
				'roleid'  	=> $roleid,
				'm'         => $value['module'],
				'c'         => $value['controller'],
				'a'         => $value['action'],
			);
		}
		$result = $this->merchantAccessDao->saveAll($data);

		if ($result !== false) {
			$this->success('权限分配成功', url('admin/merchantRole/index'));
		} else {
			$this->error($this->authAccessDao->getError());
		}
	}

	public function nodeAccess() {
		$roleid = input('param.id', '', 'intval');

		if ($roleid) {
			$info = $this->merchantRoleDao->getInfo($roleid);
		}
		$this->assign('roleid', $roleid);

		//获取节点列表
		list($nodeList) = $this->merchantNodeDao->getListWithAccess($roleid);
		//print_r($nodeList);
		$this->assign('nodeList', $nodeList);

		return $this->fetch();
	}

	public function doNodeAccess() {
		$roleid = input('post.roleid', '', 'intval');

		$menu_auth = input('post.menu_auth/a', '', 'pwEscape'); //获取所有授权菜单
		//$data['rules'] = implode(',',$menu_auth);

		//删除节点权限
		$this->merchantNodeAccessDao->where([['roleid', '=', $roleid]])->delete();

		//获取节点列表
		$ruleList = $this->merchantNodeDao->where([['id', 'in', $menu_auth]])->select();
		$ruleList = $ruleList->toArray();

		//新增节点权限
		$data = $dataCid = array();
		foreach ($ruleList as $key => $value) {
			$data[] = array(
				'roleid'   	=> $roleid,
				'flag'      => $value['flag'],
			);
		}
		$result = $this->merchantNodeAccessDao->saveAll($data);

		if ($result !== false) {
			$this->success('权限分配成功', url('admin/merchantRole/index'));
		} else {
			$this->error($this->merchantAccessDao->getError());
		}
	}

}